package com.cloud.wms.ui.in.shelf;

import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.widget.TextView;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.eventBus.Event;
import com.cloud.core.eventBus.EventBusUtil;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.in.contract.InShelfContract;
import com.cloud.wms.ui.in.entity.InCommodityEntity;
import com.cloud.wms.ui.in.entity.InOrderEntity;
import com.cloud.wms.ui.in.entity.ShelfBatchEntity;
import com.cloud.wms.ui.in.presnter.InShelfPresenter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author:Chen
 * @Date: 2021-01-26 15:28
 * @Description:
 */
public class SelectBatchActivity extends BaseBarListActivity<ShelfBatchEntity, InShelfPresenter> implements InShelfContract.View {
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private InOrderEntity inOrder;
    private InCommodityEntity commodityEntity;
    private boolean isSingle;

    @Override
    protected String barTitle() {
        return "选择批次";
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_in_shelf_select_batch;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        btnSubmit.setEnabled(false);
        inOrder = getIntent().getParcelableExtra(IntentKey.OBJECT_KEY);
        commodityEntity = getIntent().getParcelableExtra(IntentKey.INFO_KEY);
        isSingle = getIntent().getBooleanExtra(IntentKey.TYPE_KEY, false);
        mAdapter.setNewInstance(commodityEntity.getProductBatchList());
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, ShelfBatchEntity item) {
        helper.setText(R.id.tv_produce_date, item.getProductionDate().length() > 11 ? item.getProductionDate().substring(0, 11) : item.getProductionDate())
                .setText(R.id.tv_deadline_date, item.getExpirationDate().length() > 11 ? item.getExpirationDate().substring(0, 11) : item.getExpirationDate())
                .setText(R.id.tv_in_date, item.getInDate().length() > 11 ? item.getInDate().substring(0, 11) : item.getInDate())
                .setTextColorRes(R.id.tv_produce_date, item.isSelect() ? R.color.font_white : R.color.font_black)
                .setTextColorRes(R.id.tv_produce_date_name, item.isSelect() ? R.color.font_white : R.color.font_gray)
                .setTextColorRes(R.id.tv_deadline_date, item.isSelect() ? R.color.font_white : R.color.font_black)
                .setTextColorRes(R.id.tv_deadline_date_name, item.isSelect() ? R.color.font_white : R.color.font_gray)
                .setTextColorRes(R.id.tv_in_date, item.isSelect() ? R.color.font_white : R.color.font_black)
                .setTextColorRes(R.id.tv_in_date_name, item.isSelect() ? R.color.font_white : R.color.font_gray)
                .setBackgroundResource(R.id.ll_item_content, item.isSelect() ? R.drawable.bg_item_common_select : R.drawable.bg_item_common);
    }

    @Override
    protected int getResId() {
        return R.layout.activity_in_shelf_select_batch;
    }

    @Override
    protected void itemClickListener(BaseQuickAdapter<ShelfBatchEntity, BaseViewHolder> mAdapter, View view, int position) {
        super.itemClickListener(mAdapter, view, position);
        if (isSingle) {
            for (int i = 0; i < mAdapter.getData().size(); i++) {
                ShelfBatchEntity entity = mAdapter.getData().get(i);
                if (i == position) {
                    entity.setSelect(!entity.isSelect());
                } else {
                    entity.setSelect(false);
                }
            }
        } else {
            mAdapter.getData().get(position).setSelect(!mAdapter.getData().get(position).isSelect());
        }
        mAdapter.notifyDataSetChanged();
        for (int i = 0; i < mAdapter.getData().size(); i++) {
            ShelfBatchEntity entity = mAdapter.getData().get(i);
            if (entity.isSelect()) {
                btnSubmit.setEnabled(true);
                return;
            }
        }
        btnSubmit.setEnabled(false);
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
        mAdapter.setNewInstance(((ArrayList<ShelfBatchEntity>) message.obj));
    }

    @OnClick(R.id.btn_submit)
    public void onViewClicked() {
        ArrayList<ShelfBatchEntity> entities = new ArrayList<>();
        for (int i = 0; i < mAdapter.getData().size(); i++) {
            ShelfBatchEntity entity = mAdapter.getData().get(i);
            if (entity.isSelect()) {
                entities.add(entity);
            }
        }
        if (entities.isEmpty()) {
            info("请选择批次后提交");
            return;
        }
//        if (isSingle) {
//        EventBusUtil.sendEvent(new Event<>(EventConfig.SHELF_SELECT_BATCH_SINGLE, entities.get(0)));
//        } else {
        EventBusUtil.sendEvent(new Event<>(EventConfig.SHELF_SELECT_BATCH, entities.get(0)));
//        }
        this.finish();
    }

    public static void start(Context context, InOrderEntity entity, InCommodityEntity commodityEntity, boolean isSingle) {
        Intent intent = new Intent(context, SelectBatchActivity.class);
        intent.putExtra(IntentKey.OBJECT_KEY, entity);
        intent.putExtra(IntentKey.INFO_KEY, commodityEntity);
        intent.putExtra(IntentKey.TYPE_KEY, isSingle);
        context.startActivity(intent);
    }

    public static void start(Context context, InOrderEntity entity, InCommodityEntity commodityEntity) {
        Intent intent = new Intent(context, SelectBatchActivity.class);
        intent.putExtra(IntentKey.OBJECT_KEY, entity);
        intent.putExtra(IntentKey.INFO_KEY, commodityEntity);
        context.startActivity(intent);
    }
}
